//
//  CXWelcomeController.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/21.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit
import SDWebImage

class CXWelcomeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
    }
    ///  若想加载后看到明显的动画 就应该将演示动画方法写在这里
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        print("头像frame: %@", NSStringFromCGRect(headIcon.frame))
        makeAnimation()
    }
    private func setupUI() {
        
        view.backgroundColor = UIColor(white: 247/255, alpha: 1)
        
        view.addSubview(headIcon)
        view.addSubview(messageLabel)
        
        //设置约束
        headIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.view).offset(200)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headIcon.snp_bottom).offset(10)
            make.centerX.equalTo(headIcon)
        }
    }
    
    /// 把联网请求图片放在懒加载中完成，使得一旦开始使用该图片 就联网加载它
    private lazy var headIcon:UIImageView = {
        let placeholder = UIImage(named: "avatar_default_big")
        let headIv = UIImageView(image: placeholder)
        //headIv.image = placeholder
//        headIv.sizeToFit()
        //用户模型未必此时可以请求得到
        headIv.sd_setImageWithURL(NSURL(string:CXUserInfoViewModel.sharedUserModel.userInfo?.avatar_large ?? ""), placeholderImage: placeholder)
        print("头像size: %@", NSStringFromCGSize(headIv.bounds.size))
        headIv.layer.cornerRadius = 40
        headIv.layer.masksToBounds = true
        
        return headIv
    }()
    
    private lazy var messageLabel: UILabel = {
        let message = UILabel(textColor: UIColor.lightGrayColor(), font: 15.0)
        message.text = "欢迎回来"
        message.alpha = 0
        return message
    }()
}

extension CXWelcomeController {
    
    private func makeAnimation() {
        
        print("头像frame: %@", NSStringFromCGRect(headIcon.frame))
        headIcon.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(100)
        }
        //将重新布局写在动画中 进入动画前先更新约束
        ///  该方法带有弹簧效果; 头像往上移
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //标签框透明度变为1
                UIView.animateWithDuration(1.5, animations: { () -> Void in
                    
                    self.messageLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                        //发送通知 更换根控制器；界面跳转(跳转到主界面)
                        NSNotificationCenter.defaultCenter().postNotificationName(CXSwitchRootVCNotification, object: nil)
                })
        }
    }
}
